{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2fe77504",
   "metadata": {},
   "source": [
    "* 时间:2022/10/25 week 10\n",
    "*记录人：陈雯慧\n",
    "------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "298764b9",
   "metadata": {},
   "source": [
    "# 上周回顾\n",
    "\n",
    "> 1.while循环： while True -->无限循环\n",
    ">>1. 指定次数：while True 可以实现for 循环的指定次数，增加一个count计数器即可。\n",
    ">>2. while True 一般与用户输入一起使用，让用户可以重复输入，知道某些条件成立结束输入（购物车）\n",
    "> 2.购物车实践（平时作业）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7381a22f",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "> 1.元组\n",
    "2.集合\n",
    "3.字典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa99982a",
   "metadata": {},
   "source": [
    "# 1.元组（tuple）\n",
    "\n",
    "> 1. 元组的基本内容（形态、定义\n",
    "> 2. 元组的性质\n",
    ">> 1. 有序性(indexing, sliceing)\n",
    ">> 2. <font color=\"red\">**不可变性**</font>,元组没有：、删、改\n",
    "> 2. 元组的一般方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "0efecb1d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(180001, 180002, 180003) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "网三_学号 = (180001,180002,180003)\n",
    "print(网三_学号, type (网三_学号))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "609ff2d9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(190001, 190002, 190003) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "网二_学号 = 190001,190002,190003\n",
    "print (网二_学号,type(网二_学号))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "0ef1a529",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "190001"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_学号[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2a660636",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(190002, 190003)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_学号[1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "54fc8735",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2022, tm_mon=10, tm_mday=25, tm_hour=15, tm_min=6, tm_sec=33, tm_wday=1, tm_yday=298, tm_isdst=0)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#元组的实际案例\n",
    "import time\n",
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "b4f6e6cf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "([1, 2, 3, 4], [3, 4, 5, 6]) <class 'tuple'>\n",
      "[1, 2, 3, 4] 3045600340288\n"
     ]
    }
   ],
   "source": [
    "tuple_test = ([1,2,3,4],[3,4,5,6])\n",
    "print(tuple_test,type(tuple_test))\n",
    "print(tuple_test[0],id(tuple_test[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "7bbd9c50",
   "metadata": {},
   "outputs": [],
   "source": [
    "tuple_test[0].append(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "d3ebab59",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "([1, 2, 3, 4, 100], [3, 4, 5, 6]) <class 'tuple'> 3045598627648\n",
      "[1, 2, 3, 4, 100] 3045600340288\n"
     ]
    }
   ],
   "source": [
    "print(tuple_test,type(tuple_test),id(tuple_test))\n",
    "print(tuple_test[0],id(tuple_test[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "ca2f98ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "#一般情况下，元组里面存放的是 常量（不可变），何为常量：数字，字符串\n",
    "# 不存放常量 如，列表，字典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b4511b4",
   "metadata": {},
   "source": [
    "# 2.集合（sets）\n",
    "> 1. 集合的基本概念\n",
    ">> 1. 性质：没有重复项\n",
    ">> 2. 性质：无序性\n",
    "> 2. 集合的一般方法\n",
    ">> 1. 集合的运算：& ，\n",
    ">> 2. 集合的一般方法：\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "f6f04528",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'basket_lists' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-23-631207551d71>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mbasket_list\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;34m'apple'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'orange'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'apple'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'pear'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'orange'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'banana'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mbasket_sets\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;34m'apple'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'orange'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'apple'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'pear'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'orange'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'banana'\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbasket_lists\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbasket_sets\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'basket_lists' is not defined"
     ]
    }
   ],
   "source": [
    "basket_list = ['apple','orange','apple','pear','orange','banana']\n",
    "basket_sets = {'apple','orange','apple','pear','orange','banana'}\n",
    "print(basket_lists)\n",
    "print(basket_sets)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "bb36d120",
   "metadata": {},
   "outputs": [],
   "source": [
    "舞蹈社 = {1801,1802,1803,1809,1811,1812}\n",
    "电竞社 = {1808,1822,1802,1801,1888.1899}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8db252bf",
   "metadata": {},
   "source": [
    "* 集合的运算、交集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "f409d35a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1801, 1802}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "舞蹈社 & 电竞社"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09491813",
   "metadata": {},
   "source": [
    "* 集合的运算和并集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "48cf847e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1801, 1802, 1803, 1808, 1809, 1811, 1812, 1822, 1888.1899}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "舞蹈社 | 电竞社"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "71f4900f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(舞蹈社 | 电竞社)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "7d6e84fa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1801, 1802, 1803, 1808, 1809, 1811, 1812, 1822, 1888.1899}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "舞蹈社.union(电竞社)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91bd2c24",
   "metadata": {},
   "source": [
    "# 3.字典 （dict)\n",
    "> 1. 字典的基本概念\n",
    ">> 1. 字典性质：\n",
    ">>> 1. 无序性\n",
    ">>> 2. keys 是不可变的\n",
    ">>> 3. ksys 是唯一的\n",
    ">> 2. 字典的形态\n",
    "> 2. 字典的一般方法\n",
    ">> 1. 字典的创建\n",
    ">> 2. 字典的函数和方法\n",
    ">>> 1. 增加\n",
    ">>> 2. 删除\n",
    ">>> 3. 修改"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57b275cc",
   "metadata": {},
   "source": [
    "## 3.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "4bde8035",
   "metadata": {},
   "outputs": [],
   "source": [
    "网新二班 = {\n",
    "    180001:\"李一峰\",\n",
    "    180002:\"jack\",\n",
    "    180003:\"marry\",\n",
    "    180004:\"周杰伦\"\n",
    "    \n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "7b91a6f8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{180001: '李一峰', 180002: 'jack', 180003: 'marry', 180004: '周杰伦'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "print(网新二班,type(网新二班))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "9935a0c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "#因为学号是唯一的可以作为字典的key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "0712f016",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{180001: '李一峰', 180002: 'jack', 180003: 'marry', 180004: '周杰伦', 180005: 'jack'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "网新三班 = {\n",
    "    180001:\"李一峰\",\n",
    "    180002:\"jack\",\n",
    "    180003:\"marry\",\n",
    "    180004:\"周杰伦\",\n",
    "    180005:\"jack\"\n",
    "    \n",
    "}\n",
    "print(网新三班,type(网新三班))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "1d1917c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'周杰伦'"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#字典的取值：通过keys进行字典的取值，unlike列表，元组和字符串通过index取值\n",
    "网新三班[180004]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "7542bc14",
   "metadata": {},
   "outputs": [],
   "source": [
    "网新三班 = {\n",
    "    180001:[\"李一峰\",\"广州\",\"男\",19,\"聊天记录\",\"朋友圈\"],\n",
    "    180002:\"jack\",\n",
    "    180003:\"marry\",\n",
    "    180004:\"周杰伦\",\n",
    "    180005:\"jack\"\n",
    "    \n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "91c84f5a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['李一峰', '广州', '男', 19, '聊天记录', '朋友圈']"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新三班[180001]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b85524cf",
   "metadata": {},
   "source": [
    "## 3.2 字典的一般方法\n",
    "\n",
    "* 1. 按照字典的形式直接创建（key:value)\n",
    "---\n",
    "网新二班 = {\n",
    "    180001:\"李一峰\",\n",
    "    180002:\"jack\",\n",
    "    180003:\"marry\",\n",
    "    180004:\"周杰伦\"\n",
    "    \n",
    "}\n",
    "\n",
    "---\n",
    "* 2. 使用dict()方法创建字典\n",
    "\n",
    "网新四班 = dict(\n",
    "   stu180001='李一峰'\n",
    "   stu180002='jack'\n",
    "   stu180003='marry'\n",
    "   stu180004='周杰伦'\n",
    "   \n",
    "）   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "4a279901",
   "metadata": {},
   "outputs": [],
   "source": [
    "#使用dict()方法创建字典\n",
    "\n",
    "网新四班 = dict(\n",
    "   stu180001='李一峰',\n",
    "   stu180002='jack',\n",
    "   stu180003='marry',\n",
    "   stu180004='周杰伦',\n",
    "   \n",
    ")   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "babfffbe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu180001': '李一峰',\n",
       " 'stu180002': 'jack',\n",
       " 'stu180003': 'marry',\n",
       " 'stu180004': '周杰伦'}"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新四班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "6088f33f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用字典增加的方式创建字典 ： dict{key} = value\n",
    "\n",
    "网新一班 = {}\n",
    "网新一班[\"stu180001\"] = '李一峰'\n",
    "网新一班[\"stu180002\"] = 'jack'\n",
    "网新一班[\"stu180003\"] = 'marry'\n",
    "网新一班[\"stu180004\"] = '周杰伦'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "5e5d2ec8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu180001': '李一峰',\n",
       " 'stu180002': 'jack',\n",
       " 'stu180003': 'marry',\n",
       " 'stu180004': '周杰伦'}"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新一班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "fbac970e",
   "metadata": {},
   "outputs": [],
   "source": [
    "网新一班_key = [\"stu180001\",\"180002\",\"180003\",\"180004\"]\n",
    "网新一班_values = ['李一峰','jack','marry','周杰伦']\n",
    "网新一班 = {}\n",
    "for i in range (4):\n",
    "    网新一班[网新一班_key[i]] = 网新一班_values[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "6e2f3696",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu180001': '李一峰', '180002': 'jack', '180003': 'marry', '180004': '周杰伦'}"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新一班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d65b2068",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
